package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.entity.*;
import com.neuedu.mapper.CheckMapper;
import com.neuedu.mapper.DeptMapper;
import com.neuedu.service.CheckService;
import com.neuedu.vo.CheckCondition;
import com.neuedu.vo.CheckItemVO;
import com.neuedu.vo.CheckRegCondition;
import com.neuedu.vo.DoctorVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CheckServiceImpl extends ServiceImpl<CheckMapper, Check> implements CheckService {

    @Resource
    private CheckMapper checkMapper;

    @Resource
    private DeptMapper deptMapper;

    @Override
    public Page<CheckItemVO> getCheckList(CheckCondition checkCondition) {

        Page<Check> checkPage = new Page(checkCondition.getCurrentPage(), checkCondition.getPageSize());

        //创建查询包裹器
        QueryWrapper<Check> wrapper = new QueryWrapper<>();

        //填充查询条件
        wrapper.like("check_name", checkCondition.getCheckName());
        wrapper.like("check_code", checkCondition.getCheckCode());

        if(checkCondition.getMinAmount()!=null){
            wrapper.ge("check_amount", checkCondition.getMinAmount());
        }

        if(checkCondition.getMaxAmount()!=null){
            wrapper.le("check_amount", checkCondition.getMaxAmount());
        }

        if(checkCondition.getDeptId()!=null){
            wrapper.eq("dept_id", checkCondition.getDeptId());
        }

        //获取分页对象
        checkPage = checkMapper.selectPage(checkPage, wrapper);

        //创建目标分页对象
        Page<CheckItemVO> checkItemVOPage = new Page<>();

        //填充目标分页对象
        BeanUtils.copyProperties(checkPage, checkItemVOPage);

        //创建CheckItemVO列表
        List<CheckItemVO> checkItemVOList = new ArrayList<>();

        for (Check check : checkPage.getRecords()) {

            CheckItemVO checkItemVO = new CheckItemVO();

            BeanUtils.copyProperties(check, checkItemVO);

            //填充科室名称
            Dept dept = deptMapper.selectById(check.getDeptId());
            checkItemVO.setDeptName(dept.getDeptName());

            //添加到VO列表中
            checkItemVOList.add(checkItemVO);
        }

        checkItemVOPage.setRecords(checkItemVOList);

        //返回目标分页对象
        return checkItemVOPage;

    }

    @Override
    public boolean removeEnable(int checkId) {
        return checkMapper.findCheckRequestCount(checkId)==0;
    }

    @Override
    public boolean deleteBatch(String checkIds) {

        //将字符串拆分成数组  "1,2,3" --> [1,2,3]
        String[] arr = checkIds.split(",");

        //字符串数组转换集合  [1,2,3] --> {1,2,3}
        List<Integer> list = new ArrayList<>();

        for (String s : arr) {
            list.add(Integer.parseInt(s));
        }

        return this.removeByIds(list);
    }




}
